﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;

namespace Tdv.Wfrp.CharacterGenerator.Infrastructure
{
    public class SessionService : ISessionService
    {

        private ISessionFactory _sessionFactory;

        private ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    Configuration cfg = new Configuration();
                    cfg.Configure(Assembly.GetCallingAssembly(), "Tdv.Wfrp.CharacterGenerator.Infrastructure.Mappings.hibernate.cfg.xml");
                    cfg.AddAssembly(Assembly.GetCallingAssembly());
                    SessionFactory = cfg.BuildSessionFactory();
                }

                return _sessionFactory;

            }
            set
            {
                _sessionFactory = value;
            }
        }

        public ISession CreateSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
